/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.mojo;

import java.io.File;
import java.io.IOException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import com.google.code.debmavenplugin.DebianPackager;


/**
 * Mojo which creates a Debian Package.
 * FIXME: Add some config validation.
 * FIXME: Handle classifiers (see below)
 * <pre>
 * //
 * // @component
 * //
 * private MavenProjectHelper mavenProjectHelper;
 *
 * //
 * // Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
 * // @parameter
 * // private String classifier;
 * ...
 * String classifier = getClassifier();
 * if ( classifier != null ) {
 *   projectHelper.attachArtifact( getProject(), getType(), classifier, jarFile );
 * } else {
 *   getProject().getArtifact().setFile( jarFile );
 * }
 * </pre>
 * @goal deb
 * @phase package
 * @threadSafe true
 */
public class DebMojo extends AbstractMojo {

  /**
   * @parameter
   * @required
   */
  private Data data;

  /**
   * The name of the generated debian package.
   * @parameter default-value="${project.artifactId}-${project.version}.deb"
   * @required
   * @readonly
   */
  private String packageName;

  /**
   * The output directory for the generated debian package.
   * @parameter default-value="${project.build.directory}"
   * @required
   * @readonly
   */
  private File outputDir;

  /**
   * The directory containing the control files for the generated debian
   * package.
   * @parameter default-value="${project.build.directory}/classes/control-files"
   * @required
   * @readonly
   */
  private File controlFiles;


  /**
   * @parameter default-value="${project}"
   */
  private MavenProject mavenProject;


  @Override
  public void execute() throws MojoExecutionException {

    getLog().info("Start Mojo execution");

    DebianPackager packager =
      new DebianPackager(this.data.createArchiveMetaData(), this.controlFiles, this.packageName, this.outputDir);
    try {
      packager.createDebianPackage();
    } catch (IOException e) {
      throw new MojoExecutionException("Error while creating debian package '" + this.packageName + "'", e);
    }

    // Add the generated debian package to the maven project.
    this.mavenProject.getArtifact().setFile(new File(this.outputDir, this.packageName));
  }
}
